<?php
$type = $_FILES["datei"]["type"];
if (($type == "image/jpeg") || ($type == "image/jpg"))
{

      move_uploaded_file($_FILES["datei"]["tmp_name"], $photo_name);
}
else
  {
  echo "Invalid file Type!";
  }
$imagepath = $photo_name;
$imagepathsave = $photo_name_save;
$ext = strtolower(end(explode('.',$imagepath)));

  $maxwidth = 207;
  $maxheight = 166;
  $flag = 0;

//create original image
 
 switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($imagepath) ;
                  //  header('Content-Type: image/jpeg'); //set mime type content
        break;
        case 'gif':
                    $image = imagecreatefromgif($imagepath) ;
                    header('Content-Type: image/gif'); //set mime type content
        break;
        case 'png':
                    $image = imagecreatefrompng($imagepath) ;
                    header('Content-Type: image/png'); //set mime type content
        break;
    } 

//get image dimension

$dim=getimagesize($imagepath);
 $width = $dim[1];
 $height = $dim[0];

// set flag if image is sideways
if ($width < $height) { 
	$flag = 1; 
	}

//Set thumb dimension
//$thumbw = 207;
//$thumbh = 166;


//determine which side is the longest to use in calculating length of the shorter side 
// since the longest will be the max size for whichever side is longest.    
if ($height > $width) 
{   
$ratio = $maxheight / $height;  
$newheight = $maxheight;
$newwidth = $width * $ratio; 
}
else 
{
$ratio = $maxwidth / $width;   
$newwidth = $maxwidth;  
$newheight = $height * $ratio;   
}

$thumbw = $newheight;
$thumbh = $newwidth;
//create empty image

$thumb_image=imagecreatetruecolor($thumbw, $thumbh);

//Resize original image and copy it to thumb image

imagecopyresampled($thumb_image, $image, 0, 0, 0, 0,
					$thumbw, $thumbh, $dim[0], $dim[1]);

if ($flag == 1) 
{
	$degrees = -90;
	$thumb_image = imagerotate($thumb_image, $degrees, 0);
	$imager = imagejpeg($thumb_image,$imagepathsave);
	$flag = 0;
	
}else{
	
//save and display thumb image
imagejpeg($thumb_image,$imagepathsave);

}
imagedestroy($thumb_image);
unlink($imagepath);
echo "<center><img src=".$imagepathsave." alt='id badge photo'></center>";

?>